﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Algorithm_api.calculate.YachtRentals
{
    public class Yacht
    {
        public int N { get; set; }
        public int[][] prices { get; set; }

        public Yacht(int[] prices, int n)
        {
            this.prices = new int[n + 1][];
            for (int i = 0; i < n ; i++)
            {
                this.prices[i] = new int[n + 1];
                for (int j = 0; j < n ; j++)
                {
                    int poi = i * n + j;
                    if (poi < prices.Length && prices[poi]!=0)
                        this.prices[i][j] = prices[poi];
                    else
                        this.prices[i][j] = 0;
                }
            }
            N = n;
        }

        public YachtResult LowerPrice()
        {
            for (int k = 2; k < N; k++) //选取跨越距离
            {
                for (int i = 0; i <= N - k; i++)//选取起始点
                {
                    int end = i + k; //计算终点
                    for (int z = i + 1; z < end; z++) //计算中间是否分段
                    {
                        int p = prices[i][z] + prices[z][end];//计算起始点到中间点 再从中间点到结束点的总价
                        if (prices[i][end] > p) //如果更便宜
                            prices[i][end] = p;
                    }
                }
            }
            var c_prices = prices;
            YachtResult result = new YachtResult()
            {
                prices = c_prices,
                lowerPrice = prices[0][N - 1]
            };
            return result;
        }
    }
}
